package com.action.gulimall.search.controller;

import com.action.common.exception.BizCodeEnum;
import com.action.common.model.es.SkuEsModel;
import com.action.common.utils.R;
import com.action.gulimall.search.feign.ProductFeignService;
import com.action.gulimall.search.service.ProductService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProductController
 * @Deacription 产品相关
 * @Author sl
 * @Date 2021/6/18/018 10:15
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping(value = "/search/product")
public class ProductController{

    @Autowired
    ProductService productService;

    @Autowired
    ProductFeignService productFeignService;

    @PostMapping("/skuesmodel/save")
    public R saveSkuModelsToEs(@RequestBody List<SkuEsModel> skuEsModels){
        try{
            boolean b = productService.saveSkuModels(skuEsModels);
            if(b){
                return R.ok();
            }else{
                return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
            }
        }catch (Exception e){
            log.error("saveSkuModelsToEs出现异常,保存的skuEsModels为:"+ JSON.toJSONString(skuEsModels), e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
    }


    @GetMapping("/attrinfo/{attrId}")
    public R attrinfo(@PathVariable(value="attrId") Long attrId){
        return productFeignService.attrInfo(attrId);
    }


    @GetMapping("/hello")
    public R hello(){
        return R.ok();
    }
}
